package com.foreknow.demo18;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

/**
 * 我们要将查询到的数据以对象的方式保存的集合List中
 */
public class JdbcDemo {
    public static void main(String[] args) {
        try {
            Class.forName("com.mysql.jdbc.Driver");
            Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/basic2","root","123456");
            Statement stmt = conn.createStatement();
            ResultSet rs = stmt.executeQuery("select * from emp");
            List<Emp> list = new ArrayList<>();
            while (rs.next()) {
                Emp emp = new Emp();
                int empno = rs.getInt("empno");
                String ename = rs.getString("ename");
                String job = rs.getString("job");
                //将获取到的当前行数据保存到Emp对象中
                emp.setEmpno(empno);
                emp.setEname(ename);
                emp.setJob(job);
                //将当前对象保存到list中
                list.add(emp);
            }
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}
